<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>后台管理界面</title>
		<script type="text/javascript">
			function list(){
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						var arr=JSON.parse(r);
						var strHTML=`
						<table border="1" bordercolor="purple" align="center">
						<tr>
						<th>商品编号</th>
						<th>所属商品分类</th>
						<th>商品名称</th>
						<th>所属食品分类编号</th>
						<th>主标题</th>
						<th>价格</th>
						<th>尺寸</th>
						<th>重量</th>
						<th>甜度</th>
						<th>最大规格食用人数</th>
						<th>食用人数</th>
						<th>商品详细说明</th>
						<th>上架时间</th>
						<th>销量</th>
						<th>是否促销</th>
						<th>
						<button onclick="location.href='product_add.html'">添加</button>
						</th>
						`;
						for(var i=0;i<arr.length;i++){
							console.log(arr[1].weight);
							strHTML+=`
							<tr>
								<td>${arr[i].pid}</td>
								<td>${arr[i].category_id}</td>
								<td>${arr[i].name}</td>
								<td>${arr[i].tastes}</td>
								<td>${arr[i].title}</td>
								<td>${arr[i].price}</td>
								<td>${arr[i].spec}</td>
								<td>${arr[i].weigth}</td>
								<td>${arr[i].sweetness}</td>
								<td>${arr[i].max_meals_num}</td>
								<td>${arr[i].meals_num}</td>
								<td>${arr[i].details}</td>
								<td>${arr[i].shelf_time}</td>
								<td>${arr[i].sold_count}</td>
								<td>${arr[i].is_onsale}</td>
								<td>
									<a href="product_update.html?pid=${arr[i].pid}">修改</a>
									<a href="javascript:del(${arr[i].pid})">删除</a>
								</td>
								</tr>
							`;
						}
						strHTML+=`</table>`;
						d1.innerHTML=strHTML;
					}
				}
				xhr.open("GET","/product/product_list",true);
				xhr.send();
			}
			
			function del(_pid){
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("删除成功");
							list();
						}else{
							alert("删除失败");
						}
					}
				}
				xhr.open("delete","/product/product_delete/"+_pid,true);
				xhr.send();
			}
		</script>
	</head>
	<body onload="list()">
		<div id="d1"></div>
	</body>
</html>
